/**
 * 项目名称: Topic_725.cpp
 * 项目路径: study/DataStructure/LinkedList/LeetCode/链表/中等
 * 编辑时间: 2024年06月28日21时36分20秒
 * 用   户: 轻尘
 */

#include <bits/stdc++.h>

using namespace std;

struct ListNode {
    int val;
    ListNode *next;

    ListNode() : val(0), next(nullptr) {}

    ListNode(int x) : val(x), next(nullptr) {}

    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    vector<ListNode *> splitListToParts(ListNode *head, int k) {
        vector<ListNode *> result;
        int p[1010];
        int cnt = 0;
        ListNode *po = head;
        while (po) {
            p[cnt++] = po->val;
            po = po->next;
        }

        if (cnt <= k) {
            for (int i = 0; i < k; i++) {
                if (i < cnt)result.push_back(new ListNode(p[i]));
                else result.push_back(nullptr);
            }
        } else {
            int pr = cnt / k;
            int pp = cnt % k;
            cout << cnt << " " << pr << " " << pp << endl;

            int tmp[pr + 1];
            tmp[0]=0;
            for (int i = 1; i <= pr; i++) {
                if (pp > 0)tmp[i] = pr + 1;
                else tmp[i] = pr;
                pp--;
            }
            int sum = 0;
            for (int i = 1; i <= pr; i++){
                ListNode * pq = nullptr;
                int num = tmp[i];
                for(int j=0;j<num;j++){
                    ListNode * pi = new ListNode;
                    pi->val = p[sum + j];
                    if(!pq)pq = pi;
                    else{
                        ListNode * pu = pq;
                        while (pu->next)pu = pu->next;
                        pu->next = pi;
                    }
                }
                sum+=num;
                result.push_back(pq);
            }
        }

        return result;
    }
};

int main() {

    return 0;
}
